/*

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#import "NowPlayingView.h"
#import "XBMCControlApplication.h"
#import "XBMCHttpInterface.h"
#import "IconManager.h"
#import "PlaybackControlView.h"

#ifdef __OBJC2__ 
#import <UIKit/UIOldSliderControl.h>
#define SLIDER_CONTROL_TYPE UIOldSliderControl  
#else
#import <UIKit/UISliderControl.h>
#define SLIDER_CONTROL_TYPE UISliderControl  
#endif



@implementation NowPlayingView

- (id) initWithApplication: (UIApplication*)app withAppID: (NSString*)appID withFrame: (struct CGRect)rect
{
    self = [super initWithFrame: rect];
	m_viewIsActive = NO;
	m_rect = rect;
	m_xbmc = [XBMCHttpInterface sharedInstance];
	m_mainApp = (XBMCControlApplication*)app;
	
	// playback control view
	m_playbackControlView = [[PlaybackControlView alloc] initWithObserver: self withFrame: rect];
	[self addSubview: m_playbackControlView];
	
	// nav bar
    m_navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, rect.size.width, 48.0f)] autorelease];
    [m_navBar showLeftButton:@"Main Menu" withStyle:0 rightButton:@"Refresh" withStyle:3];
    [m_navBar setBarStyle:0];
    [m_navBar setDelegate:self];
	[self addSubview: m_navBar];
	
    // pref table
    m_table = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 48.0f, rect.size.width, rect.size.height - 48.0f - [m_playbackControlView getHeight])];
    [m_table setDataSource:self];
    [m_table setDelegate:self];
    [self addSubview: m_table];
	
	/*
	
	// JOEBUG: moved to playbackControlView!!!!1!!!!one!!!!
	
	// push buttons

	// prev track
	m_prevTrackButton = [[UIPushButton alloc] initWithTitle:@"" autosizesToFit:NO];
	[m_prevTrackButton setFrame: CGRectMake((rect.size.width/2 - playPauseButtonSize/2 - prevNextButtonSize - 4) , rect.size.height - playPauseButtonSize +(playPauseButtonSize - prevNextButtonSize)/2, prevNextButtonSize, prevNextButtonSize)];
	[m_prevTrackButton setDrawsShadow: YES];
	[m_prevTrackButton setEnabled:YES];  //may not be needed
	[m_prevTrackButton setStretchBackground:YES];

	[m_prevTrackButton setBackground:[[IconManager sharedInstance] getPrevTrackIcon] forState:0];  //up state
	[m_prevTrackButton setBackground:[[IconManager sharedInstance] getPrevTrackIcon_Pressed] forState:1]; //down state
	[m_prevTrackButton addTarget:self action:@selector(prevTrackSelected) forEvents:(64)];
	[self addSubview: m_prevTrackButton];
	
	// play pause
	m_playPauseButton = [[UIPushButton alloc] initWithTitle:@"" autosizesToFit:NO];
	[m_playPauseButton setFrame: CGRectMake(rect.size.width/2 - playPauseButtonSize/2, rect.size.height - playPauseButtonSize, playPauseButtonSize, playPauseButtonSize)];
	[m_playPauseButton setDrawsShadow: YES];
	[m_playPauseButton setEnabled:YES];  //may not be needed
	[m_playPauseButton setStretchBackground:YES];

	[m_playPauseButton setBackground:[[IconManager sharedInstance] getPlayPauseIcon] forState:0];  //up state
	[m_playPauseButton setBackground:[[IconManager sharedInstance] getPlayPauseIcon_Pressed] forState:1]; //down state
	[m_playPauseButton addTarget:self action:@selector(playPauseSelected) forEvents:(64)];
	[self addSubview: m_playPauseButton];
	
	// next track
	m_nextTrackButton = [[UIPushButton alloc] initWithTitle:@"" autosizesToFit:NO];
	[m_nextTrackButton setFrame: CGRectMake(rect.size.width/2 + playPauseButtonSize/2 + 4, rect.size.height - playPauseButtonSize +(playPauseButtonSize - prevNextButtonSize)/2, prevNextButtonSize, prevNextButtonSize)];
	[m_nextTrackButton setDrawsShadow: YES];
	[m_nextTrackButton setEnabled:YES];  //may not be needed
	[m_nextTrackButton setStretchBackground:YES];

	[m_nextTrackButton setBackground:[[IconManager sharedInstance] getNextTrackIcon] forState:0];  //up state
	[m_nextTrackButton setBackground:[[IconManager sharedInstance] getNextTrackIcon_Pressed] forState:1]; //down state
	[m_nextTrackButton addTarget:self action:@selector(nextTrackSelected) forEvents:(64)];
	[self addSubview: m_nextTrackButton];
	*/
	
	 // create the pref table cells
	 //   title
    m_titleCell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_titleCell setTitle:@"Title"];
    [m_titleCell setValue:@"Unknown Title"];
	//   artist
	m_artistCell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_artistCell setTitle:@"Artist"];
    [m_artistCell setValue:@"Unknown Artist"];
	//   album
	m_albumCell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_albumCell setTitle:@"Album"];
    [m_albumCell setValue:@"Unknown Album"];
	//   filename
	m_filenameCell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_filenameCell setTitle:@"Filename"];
    [m_filenameCell setValue:@"Unknown Filename"];
	
	// volume control
	m_volumeCell = [[UIPreferencesControlTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, rect.size.width, 48.0f)];
    [m_volumeCell setTitle:@"Volume"];
    SLIDER_CONTROL_TYPE *volumeControl = [[[SLIDER_CONTROL_TYPE alloc] initWithFrame: CGRectMake(rect.size.width - 215.0f, 2.0f, 200.0f, 40.0f)] autorelease];
    [volumeControl setContinuous: YES];
    [volumeControl setMinValue: 0.0f];
    [volumeControl setMaxValue: 100.0f];
    float volumeF = 100.0f;
    [volumeControl setValue: volumeF];
	[volumeControl addTarget:self action:@selector(volumeChanged) forEvents: /*7|64*/ (1<<6)];
    [m_volumeCell setControl:volumeControl];
    	
	UINavigationItem *title = [[[UINavigationItem alloc] initWithTitle:@"Now Playing"] autorelease];
    [m_navBar pushNavigationItem:title];

	return self;
}

-(void) refreshData
{
	[m_mainApp showProgress: YES];
	// get song playing
	NSMutableDictionary *itemInfo = [m_xbmc getCurrentlyPlayingInfo];
	if (itemInfo == nil)
	{
		[m_titleCell setValue:@"(Nothing Playing)"];
	    [m_artistCell setValue:@""];
	    [m_albumCell setValue:@""];
		[m_filenameCell setValue:@""];
	}
	else
	{
		[m_titleCell setValue:[itemInfo valueForKey:@"Title"]];
		[m_artistCell setValue:[itemInfo valueForKey:@"Artist"]];
		[m_albumCell setValue:[itemInfo valueForKey:@"Album"]];
		[m_filenameCell setValue:[itemInfo valueForKey:@"Filename"]];
	}
	
	// get volume
	float volume = [m_xbmc getVolume];
	SLIDER_CONTROL_TYPE *volumeControl = [m_volumeCell control];
	[volumeControl setValue: volume];
    	
	[m_table reloadData];
	[m_mainApp showProgress: NO];
}

- (void) volumeChanged
{
	[m_mainApp showProgress: YES];
	SLIDER_CONTROL_TYPE *volumeControl = [m_volumeCell control];
	[m_xbmc setVolume:[volumeControl value]];
	[m_mainApp showProgress: NO];
}

- (void)observePlayPause
{
	//n othing
}

- (void)observePrevTrack
{
	[self refreshData];
}

- (void)observeNextTrack
{
	[self refreshData];
}

- (void)showProgress: (BOOL)show
{
	[m_mainApp showProgress: show];
}

/*
- (void) playPauseSelected
{
	[m_mainApp showProgress: YES];
	[m_xbmc pause];
	[m_mainApp showProgress: NO];
}

- (void) nextTrackSelected
{
	[m_mainApp showProgress: YES];
	[m_xbmc playNext];
	[self refreshData];
	[m_mainApp showProgress: NO];
}

- (void) prevTrackSelected
{
	[m_mainApp showProgress: YES];
	[m_xbmc playPrev];
	[self refreshData];
	[m_mainApp showProgress: NO];
}
*/

- (int) numberOfGroupsInPreferencesTable: (id) table
{
    return 2;
}

- (int) preferencesTable:(id)table numberOfRowsInGroup:(int)group
{
    switch (group)
    {
        case 0:
            return 4;
		case 1:
			return 1;
    }
	// invalid group!
	return -1;
}

- (id)preferencesTable: (id)table cellForRow: (int) row inGroup:(int)group
{
    switch (group) {
        case 0:
			switch(row){
				case 0: return m_titleCell;
				case 1: return m_artistCell;
				case 2: return m_albumCell;
				case 3: return m_filenameCell;
			}
		case 1:
			return m_volumeCell;
    }
	// invalid group!
	return nil;
}

- (id)preferencesTable:(id)table titleForGroup:(int)group
{
    switch (group)
    {
        case 0:
            return @"Song Info";
		case 1:
            return @"Playback";
    }
	// invalid group!
	return nil;
}

-(void)dealloc
{
   [super dealloc];
}

-(void)activateView
{
   NSLog(@"activateView");
   if (m_viewIsActive == NO)
   {
	   m_viewIsActive = YES;
	   [self refreshData];
   }
   NSLog(@"activateView done");
}

-(void)deactivateView
{
   if (m_viewIsActive == YES)
   {
	   m_viewIsActive = NO;
   }
}


- (void)navigationBar:(id)navbar buttonClicked:(int)button
{
    if (navbar == m_navBar)
	{
	    switch (button)
	    {
	        case 0:
	            // refresh
				[self refreshData];
	            break;
	        case 1:
	            // transition to main menu
				[m_mainApp transitionToMainMenu];
	            break;
	    }
	}
}

@end

